package com.ky.manage.activity;

import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.google.gson.Gson;
import com.ky.manage.R;
import com.ky.manage.base.BaseActivity;
import com.ky.manage.model.DocInfo;
import com.ky.manage.model.response.MountingsInfoResp;
import com.ky.manage.model.response.ProductInfoResp;
import com.ky.manage.utils.ImageLoader.GlideImageLoader;
import com.ky.manage.utils.http.AsyncHttpUtils;
import com.ky.manage.utils.http.BusinessHttpReqUtils;
import com.loopj.android.http.TextHttpResponseHandler;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.zzhoujay.richtext.ImageHolder;
import com.zzhoujay.richtext.RichText;

import java.util.ArrayList;
import java.util.List;

import cz.msebera.android.httpclient.Header;

public class GoodsDetailActivity extends BaseActivity {

    Banner banner;
    TextView tvProductName, tvPrice, tvSalePrice, tvRichText;
    int id = -1;
    int type = 0; // 1 商品  2 配件
    List<String> images = new ArrayList<>();

    @Override
    protected int getLayoutId() {
        return R.layout.activity_product_details;
    }

    @Override
    protected void initView() {
        findView(R.id.back_iv, v -> {
            finish();
        });


        findTextView(R.id.page_title_tv, "商品详情");

        banner = findView(R.id.banner_product);
        tvProductName = findView(R.id.tv_product_name);
        tvSalePrice = findView(R.id.tv_sale_price);
        tvPrice = findView(R.id.tv_price);
        tvRichText = findView(R.id.tv_headline_content);
    }

    @Override
    protected void initData() {
        type = getIntent().getIntExtra("type", 0);
        id = getIntent().getIntExtra("id", -1);
        RichText.initCacheDir(this);
        if (id != -1) {
            switch (type) {
                case 1:
                    reqGoodsInfo();
                    break;
                case 2:
                    reqMountingsInfo();
                    break;
                default:
                    break;
            }
        }
    }

    private void reqGoodsInfo() {
        AsyncHttpUtils.getInstance().get(BusinessHttpReqUtils.SERVICE_DOMAIN + "/appApi/getGoodsDetails/" + id, new TextHttpResponseHandler() {
            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                Log.d("lhm", "reqGoodsInfo onFailure = " + responseString);
            }

            @Override
            public void onSuccess(int statusCode, Header[] headers, String responseString) {
                Log.d("lhm", "reqGoodsInfo onSuccess = " + responseString);
                ProductInfoResp resp = new Gson().fromJson(responseString, ProductInfoResp.class);
                if(resp != null && resp.getCode() == 200) {
                    if(resp.getData().getDocList() != null && resp.getData().getDocList().size() > 0) {
                        for(Object docInfo : resp.getData().getDocList()) {
                            if(!TextUtils.isEmpty(((DocInfo)docInfo).docUrl)){
                                images.add(((DocInfo)docInfo).docUrl);
                            }
                        }
                        banner.setImages(images);
                        banner.setImageLoader(new GlideImageLoader());
                        //设置指示器的位置，小点点，左中右。
                        banner.setIndicatorGravity(BannerConfig.CENTER)
                                //以上内容都可写成链式布局，这是轮播图的监听。比较重要。方法在下面。
                                .setOnBannerListener(position -> {
                                    //广告点击
                                    //ToastUtils.showRoundRectToast("广告点击 todo");
                                })
                                //必须最后调用的方法，启动轮播图。
                                .start();
                    }
                    updateView(resp);
                }
            }
        });
    }

    ///appApi/getMountingsInfo/
    private void reqMountingsInfo() {
        AsyncHttpUtils.getInstance().get(BusinessHttpReqUtils.SERVICE_DOMAIN + "/appApi/getMountingsInfo/" + id, new TextHttpResponseHandler() {
            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                Log.d("lhm", "reqMountingsInfo onFailure = " + responseString);
            }

            @Override
            public void onSuccess(int statusCode, Header[] headers, String responseString) {
                Log.d("lhm", "reqMountingsInfo onSuccess = " + responseString);
                MountingsInfoResp mountings = new Gson().fromJson(responseString, MountingsInfoResp.class);
                if (mountings != null && mountings.getCode() == 200) {
                    images.add((String) mountings.getData().getPicUrl());
                    banner.setImages(images);
                    banner.setImageLoader(new GlideImageLoader());
                    //设置指示器的位置，小点点，左中右。
                    banner.setIndicatorGravity(BannerConfig.CENTER)
                            //以上内容都可写成链式布局，这是轮播图的监听。比较重要。方法在下面。
                            .setOnBannerListener(position -> {
                                //广告点击
                                //ToastUtils.showRoundRectToast("广告点击 todo");
                            })
                            //必须最后调用的方法，启动轮播图。
                            .start();
                    updateView(mountings);
                }
            }
        });
    }

    private void updateView(MountingsInfoResp mountings) {
        //double price = mountings.getData().getUnitPrice();
        tvProductName.setText(mountings.getData().getMountingsName());
        tvSalePrice.setText("¥" + mountings.getData().getUnitPrice());

        tvPrice.setVisibility(View.GONE);
        RichText.from(mountings.getData().getContent()).bind(this)
                .showBorder(false)
                .size(ImageHolder.MATCH_PARENT, ImageHolder.WRAP_CONTENT)
                .into(tvRichText);
    }

    private void updateView(ProductInfoResp productInfo) {
        //double price = mountings.getData().getUnitPrice();
        tvProductName.setText(productInfo.getData().getGoodsName());
        tvSalePrice.setText("¥" + productInfo.getData().getSellPrice());

        tvPrice.setVisibility(View.GONE);
        RichText.from(productInfo.getData().getGoodsDetails()).bind(this)
                .showBorder(false)
                .size(ImageHolder.MATCH_PARENT, ImageHolder.WRAP_CONTENT)
                .into(tvRichText);
    }
}
